package TreeSeries;

import java.util.LinkedList;
import java.util.Queue;

public class InvertTreeSolution_226 {
    public TreeNode invertTree_DFS(TreeNode root) {
        // 递归法
        if(root == null) {
            return root;
        }

        swap(root);

        invertTree_DFS(root.right);
        invertTree_DFS(root.left);

        return root;
    }

    public TreeNode invertTree_BFS(TreeNode root) {
        if(root == null) {
            return root;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            TreeNode tempNode = queue.poll();

            if(tempNode.left!=null) queue.offer(tempNode.left);
            if(tempNode.right!=null) queue.offer(tempNode.right);

            swap(tempNode);
        }
        return root;
    }

    public void swap(TreeNode root) {
        TreeNode tempNode = root.left;
        root.left = root.right;
        root.right = tempNode;
    }
}
